package com.cat.model.web.action.admin.system;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alonew.core.commons.page.AjaxObject;
import com.alonew.core.commons.page.Page;
import com.alonew.json.JSONArray;
import com.alonew.json.JSONException;
import com.alonew.json.JSONObject;
import com.cat.model.biz.logic.SystemLogic;
import com.cat.model.biz.service.admin.SystemService;
import com.cat.model.common.model.admin.AdminModuleVO;
import com.cat.model.common.util.ErrorCode;
import com.cat.model.dao.common.PlmCommDAO;
import com.cat.model.web.action.base.AdminAction;

public class AdminModuleManagerAction extends AdminAction{

	@Autowired
	private PlmCommDAO plmCommDAO;
	
	@Autowired
	private SystemService systemService;
	
	@Autowired
	private SystemLogic systemLogic;
	
	private static final long serialVersionUID = 425636316597986569L;
	
	private AdminModuleVO adminModule;
	private Long id;
	private String sysCode;
	private String parentId;
	
	/**
	 * list
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String list() {
		Map<String, Object> condition = new HashMap<String, Object>();
		
		if(StringUtils.isNotEmpty(sysCode)){
			condition.put("sysCode", sysCode);
		}
		
		if(StringUtils.isNotEmpty(parentId)){
			condition.put("parentId", parentId);
		}
		
		Page page = this.fetchPageParams();
		page.setNumPerPage(20);
		
		Long totalCount = plmCommDAO.searchSize("manager.searchAdminModuleSize", condition);
		page.setTotalCount(totalCount);
		int start = (page.getPageNum() -1) * page.getNumPerPage();
		if(start < 0){
			start = 0;
		}
		
		List<AdminModuleVO> list = (List<AdminModuleVO>)plmCommDAO.search("manager.searchAdminModule", condition, start, page.getNumPerPage());
		
		request.setAttribute("list", list);
		request.setAttribute("size", totalCount);
		return "list";
	}
	
	public String deleteSingle() {
		try {
			systemLogic.deleteAdminModule(id);
		} catch (Exception e) {
			e.printStackTrace();
			this.print(AjaxObject.newError(e.getMessage()).setCallbackType("").toString());
		}
		this.print(AjaxObject.newOk("删除成功！").setCallbackType("").toString());
		return "list";
	}
	
	public String toAdd(){
		return "add";
	}
	
	public String add(){
		try { 
			systemService.addAdminModule(adminModule);
		} catch (Exception e) {
			e.printStackTrace();
			this.printHTML("<script>parent.callback(" + false + ", '"+e.getMessage()+"')</script>");
			return "list";
		}
		this.printHTML("<script>parent.callback(" + true + ", '保存成功！')</script>");
		return "list";
	}
	
	public String toEdit(){
		adminModule = systemLogic.getAdminModule(id);
		return "edit";
	}
	
	public String edit(){
		try { 
			systemService.updateAdminModule(adminModule);
		} catch (Exception e) {
			e.printStackTrace();
			this.printHTML("<script>parent.callback(" + false + ", '"+e.getMessage()+"')</script>");
			return "list";
		}
		this.printHTML("<script>parent.callback(" + true + ", '保存成功！')</script>");
		return "list";
	}
	
	public void getModule(){
		JSONObject json = ErrorCode.OK.responseJSON();		
		try {
			JSONObject wkJson = new JSONObject();
			List<AdminModuleVO> moduleList =  systemLogic.getAllAdminModule(sysCode);
			JSONArray moduleArray = new JSONArray();
			if(moduleList != null && moduleList.size() > 0){
				for(AdminModuleVO module : moduleList){				
					JSONObject detailJson = new JSONObject();
					detailJson.put("id", module.getId());
					detailJson.put("parentId", module.getParentId());
					detailJson.put("name", module.getName());
					detailJson.put("EnableFlag", module.getEnableFlag());
					detailJson.put("description", module.getDescription());
					
					moduleArray.put(detailJson);
				}
			}
			wkJson.put("moduleList", moduleArray);
			
			json.put("data", wkJson);
		} catch (JSONException e) {
			print(ErrorCode.RESOURCE_NO_EXIST.response());
		}
		print(json.toString());
	}
	
	public void doGetPrivilegeTree() throws IOException{
		List<AdminModuleVO> list = systemLogic.getAllAdminModule(null);
		List<String> lstTree = new ArrayList<String>();
		String s1 = "{id:0, pId:0, name:\"菜单管理\" , open:true}"; 
		lstTree.add(s1); 
		if(list!=null && list.size()>0){
			for (int i = 0; i < list.size(); i++) {
				AdminModuleVO module = (AdminModuleVO)list.get(i);
				String s = "{id:\""+module.getId()+"\", pId:\""+module.getParentId()+"\", name:\""+module.getName()+"\" , open:true}";
				if(!"0".equals(module.getParentId())){
					s = "{id:\""+module.getId()+"\", pId:\""+module.getParentId()+"\", name:\""+module.getName()+"\"}";
				} 
				 lstTree.add(s);
			}
		} 
        response.getWriter().print(net.sf.json.JSONArray .fromObject(lstTree).toString());  
    }
	
	public void getModuleById(){
		JSONObject json = ErrorCode.OK.responseJSON();		
		try {
			JSONObject wkJson = new JSONObject();
			AdminModuleVO module= systemLogic.getAdminModule(id);
			if(module != null ){		
					JSONObject detailJson = new JSONObject();
					detailJson.put("id", module.getId());
					detailJson.put("parentId", module.getParentId());
					detailJson.put("name", module.getName());
					detailJson.put("EnableFlag", module.getEnableFlag());
					detailJson.put("description", module.getDescription());
					detailJson.put("url", module.getUrl());
					detailJson.put("sysCode", module.getSysCode());
					detailJson.put("orderIndex", module.getOrderIndex());
					wkJson.put("module", detailJson);
			}
			
			json.put("data", wkJson);
		} catch (JSONException e) {
			print(ErrorCode.RESOURCE_NO_EXIST.response());
		}
		print(json.toString());
	}
	
	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}

	public String getSysCode() {
		return sysCode;
	}

	public void setSysCode(String sysCode) {
		this.sysCode = sysCode;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public AdminModuleVO getAdminModule() {
		return adminModule;
	}

	public void setAdminModule(AdminModuleVO adminModule) {
		this.adminModule = adminModule;
	}



}
